import logging
from datetime import datetime, timedelta
from pathlib import Path

from tools.ERP上传 import 退件订单不拆分上传, 预包扫码发货中上传
from tools.login_聚水潭 import login_聚水潭不拆分

logger = logging.getLogger(__name__)


def 预包扫码_发货中(page, date_hour, timestamp):
    page.set.download_file_name(f'预包扫码_发货中{date_hour}.xlsx')
    page.set.when_download_file_exists('overwrite')
    date = datetime.now().strftime('%Y-%m-%d')
    date_start = (datetime.now() - timedelta(days=3)).strftime('%Y-%m-%d %H:%M:%S')
    date_end = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    login_聚水潭不拆分(page)
    logger.info('开始操作聚水潭导出订单')
    page.wait(1)
    if page.s_eles('text=知道了'):
        page('text=知道了').click()
    page('订单').click()
    page.wait.ele_displayed('#other_qs', timeout=180)
    page.wait(5)
    x_eles = page.eles('#pop_close')
    for x_ele in x_eles:
        x_ele.click(by_js=True)
    page('发货中(打单拣货)').click(by_js=True)
    page('#wms_co_name').click()
    page.wait(1)
    page('#t14029034').click(by_js=True)
    page('#t13799183').click(by_js=True)
    page('.btn_1').click(by_js=True)
    page('搜付款时间').click(by_js=True)
    page('#order_date_begin').input(date_start, clear=True, by_js=True)
    page('#order_date_end').input(date_end, clear=True, by_js=True)
    # 点击搜索
    page('.btn_search').click()
    page.wait(2)
    if page.eles('text=暂无数据...', timeout=2):
        logger.info('发货中无数据')
        return '无数据'
    if not page.eles('._jt_row _jt_rh', timeout=2):
        logger.info('发货中无数据')
        return '无数据'
    page.wait(5)
    # 导出订单
    logger.info('开始导出订单')
    page('.ding_db_txt ding_db_txt_hover ding_db_pic').click(by_js=True)
    page.wait(3)
    eles = page.eles('#confirm_confirm')
    for ele in eles:
        ele.click(by_js=True)
        page.wait(1)
    eles = page.eles('#msg_close')
    for ele in eles:
        ele.click(by_js=True)
        page.wait(1)
    page.wait(2)
    # 进入下载订单页面
    logger.info('进入下载订单页面')
    page('.ant-badge').click(by_js=True)
    page.wait(5)
    logger.info('聚水潭导出进度开始加载')
    for i in range(1800):
        tb = page.eles('#_jt_body_list')[1]
        tr_list = tb.eles('._jt_row _jt_rh')
        if tr_list[0].ele('100%', timeout=1):
            logger.info('聚水潭导出进度100%')
            break
        if i == 1799:
            logger.info('聚水潭导出进度超时')
            raise Exception('聚水潭导出进度超时')
    # 下载excel
    tb = page.eles('#_jt_body_list')[1]
    tr_list = tb.eles('._jt_row _jt_rh')
    tr_list[0]('.btn downLoad').click()
    logger.info('等待订单文件下载')
    page.wait.download_begin(timeout=60)
    page.wait.downloads_done(timeout=60)
    logger.info('订单文件下载成功')
    for i in range(len(page.eles("下载完成"))):
        eles = page.eles("._jt_row _jt_rh")
        for ele in eles:
            if '下载完成' in ele.text:
                ele.ele(".btn del").click()
                page.wait(2)
                break
    Path(f'C:/退件新增订单/{date}').mkdir(parents=True, exist_ok=True)
    Path(f'C:/filedownloads/预包扫码_发货中{date_hour}.xlsx').replace(
        Path(f'C:/退件新增订单/{date}/预包扫码_发货中{date_hour}.xlsx'))
    logger.info(f'文件上传ERP')
    msg = 预包扫码发货中上传(f'C:/退件新增订单/{date}/预包扫码_发货中{date_hour}.xlsx', date_hour, timestamp)
    return msg
